package com.xudow.app.ui.user;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.xudow.app.R;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.task.user.RelatedAccountAddTask;
import com.xudow.app.util.Maps;
import com.xudow.app.util.StringUtils;

import java.util.HashMap;

/**
 * 添加关联用户
 *
 * @author lukecheng
 */
public class AccountRelateAddActivity extends BaseActivity {

    private RelatedAccountAddTask relatedAccountAddTask;

    private EditText usernameEditText, passwordEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_relate_add);

        usernameEditText = (EditText) findViewById(R.id.username);
        passwordEditText = (EditText) findViewById(R.id.password);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    public void onAddClick(View view) {
        String username = usernameEditText.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            usernameEditText.setError(getString(R.string.username_required));
            return;
        }

        String password = passwordEditText.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            passwordEditText.setError(getString(R.string.password_required));
            return;
        }

        HashMap<String, String> params = Maps.newHashMap();
        params.put("username", username);
        params.put("password", password);

        showLodingDialog(getString(R.string.processing));

        relatedAccountAddTask = new RelatedAccountAddTask(this, relatedAccountAddTaskHandler);
        relatedAccountAddTask.execute(params);
    }

    private Handler relatedAccountAddTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (RelatedAccountAddTask.SUCCESS == msg.what) {
                String message = getString(R.string.bind_success);
                Toast.makeText(AccountRelateAddActivity.this, message, Toast.LENGTH_SHORT).show();

                finish();
            } else {
                String message = msg.getData().getString("errMessage");
                if (StringUtils.isEmpty(message)) {
                    message = getString(R.string.bind_fail);
                }

                Toast.makeText(AccountRelateAddActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (null != relatedAccountAddTask && !relatedAccountAddTask.isCancelled()) {
                relatedAccountAddTask.cancel(true);
                relatedAccountAddTask = null;
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
